﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>参数设置</title>

    <link href="../css/site.css" rel="stylesheet" />
    <style type="text/css">
        .divContent {
            width: 100%;
            height: 100%;
            /*margin-left: auto;
            margin-right:auto;*/
            position: absolute;
            right: 0;
            left: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            /*background-color:red;*/
        }

        .inputPara {
            margin-left: -6px;
            width: 100%;
        }
    </style>

    <script src="../lib/jquery/dist/jquery.js"></script>
    <script src="../js/config.js"></script>
    <script src="../js/site.js"></script>
    <script type="text/javascript">
        function setGroupShow(groupId) {
            var index = 1;
            var tb = document.getElementById("tbParas");
            for (var i = 1; i < tb.rows.length; i++) {
                var trId = "#" + tb.rows[i].getAttribute("id");

                if (groupId == constDefine.paraGroupAll) {
                    tb.rows[i].cells[0].innerText = index++;
                    $(trId).show();
                }
                else {
                    var trGroupId = Number(tb.rows[i].getAttribute("data-groupid"));
                    if (groupId == trGroupId) {
                        tb.rows[i].cells[0].innerText = index++;
                        $(trId).show();
                    }
                    else {
                        $(trId).hide();
                    }
                }
            }
        }

        function refreshParas() {
            try {
                var prjId = Number(sessionStorage.getItem(constDefine.prjIdKey));
                var url = config.conf.baseUrl + "ParaValue/projectID=" + prjId + "&version=-1";
                var token = sessionStorage.getItem('token');
                $.ajax({
                    type: "GET",
                    url: url,
                    headers:
                    {
                        "access_token": token//此处放置请求到的用户token
                    },
                    contentType: "application/json; charset=utf-8",
                    //dataType: "json",
                    success: function (data, status) {
                        var comboxParaGroup = $("#comboxParaGroup");
                        comboxParaGroup.empty();//清空
                        clearTable("tbParas", true);

                        if (data.status != constDefine.DB_SUCESS) {
                            alert("查询参数列表失败,原因:" + data.reason);
                            return;
                        }

                        //组
                        var paraGroupDic = new Array();
                        comboxParaGroup.append('<option value="' + constDefine.paraGroupAll + '">全部</option>'); //为Select追加一个Option(下拉项)
                        for (var i = 0; i < data.value.paraGroups.length; i++) {
                            var paraGroupItem = data.value.paraGroups[i];
                            paraGroupDic[paraGroupItem.id] = paraGroupItem.name;
                            comboxParaGroup.append('<option value="' + paraGroupItem.id + '">' + paraGroupItem.name + '</option>'); //为Select追加一个Option(下拉项)
                        }

                        var tbParas = $("#tbParas");
                        for (var i = 0; i < data.value.items.length; i++) {
                            var paraItem = data.value.items[i];
                            var row = $('<tr id="tr_' + paraItem.id + '" data-groupid="' + paraItem.groupID + '" data-paraId="' + paraItem.id + '"></tr>');

                            var tdId = $("<td></td>");
                            tdId.append(i + 1);
                            row.append(tdId);

                            var tdparaGropuName = $('<td></td>');
                            tdparaGropuName.append(paraGroupDic[paraItem.groupID]);
                            row.append(tdparaGropuName);

                            var tdparaKey = $('<td></td>');
                            tdparaKey.append(paraItem.key);
                            row.append(tdparaKey);

                            var tdparaName = $('<td id="td_' + paraItem.id + '_name"></td>');
                            tdparaName.append(paraItem.name);
                            row.append(tdparaName);

                            var tdparaGroupDes = $("<td></td>");
                            tdparaGroupDes.append(paraItem.des);
                            row.append(tdparaGroupDes);

                            var tdvalue = $("<td></td>");
                            var editHtml = '<input type="text" class="inputPara inputPara" id="txtValue' + paraItem.id + '" value="' + paraItem.value + '"/>';
                            tdvalue.append(editHtml);
                            row.append(tdvalue);

                            tbParas.append(row);
                        }
                    },
                    error: function (xhr) {
                        ajaxErr(xhr);
                    }
                });
            }
            catch (ex) {
                alert(ex.error);
            }
        }

        function getParaValues() {
            //{"PID":123,"ParaValueSettings":[{"Id":1,"Value":"sadf"},{"Id":2,"Value":"132"}]}
            var prjId = Number(sessionStorage.getItem(constDefine.prjIdKey));
            var tb = document.getElementById("tbParas");
            var data = '{"PID":' + prjId + ',"ParaValueSettings":[';

            var lastRowIndex = tb.rows.length - 1;
            for (var i = 1; i < tb.rows.length; i++) {
                var trId = "#" + tb.rows[i].getAttribute("id");
                var paraId = Number(tb.rows[i].getAttribute("data-paraId"));
                var value = tb.rows[i].cells[5].children[0].value;

                //组装参数值
                var itemData = '{"Id":' + paraId + ',"Value":"' + value + '"}';
                if (i < lastRowIndex) {
                    itemData = itemData + ',';
                }

                data = data + itemData;
            }

            data = data + ']}';
            return data;
        }

        $(document).ready(function () {
            refreshParas();

            //为Select添加事件，当选择其中一项时触发
            $("#comboxParaGroup").change(function () {
                var paraGroupId = $("#comboxParaGroup").val(); //获取Select选择的Value
                setGroupShow(paraGroupId);
            });

            $("#btnParaValueOk").click(function () {
                var data = getParaValues();
                var url = config.conf.baseUrl + "ParaValue";
                var token = sessionStorage.getItem('token');
                $.ajax({
                    type: "POST",
                    url: url,
                    data: data,
                    contentType: "application/json; charset=utf-8",
                    headers:
                    {
                        "access_token": token//此处放置请求到的用户token
                    },
                    //dataType: "json",
                    success: function (data, status) {
                        if (data.status != constDefine.DB_SUCESS) {
                            alert("设置参数值失败,原因:" + data.reason);
                            return;
                        }

                        alert("设置参数值成功，参数版本号:" + data.value);
                    },
                    error: function (xhr) {
                        ajaxErr(xhr);
                    }
                });
            });
        });
    </script>
</head>
<body class="contentBackgroundColor">
    <div class="divContent">
        <div>
            <span class="spanLabel_TextAlign_Right">参数分组</span>
            <span>
                <select id="comboxParaGroup" class="select">
                    <!--<option value="volvo">Volvo</option>
                    <option value="saab">Saab</option>
                    <option value="opel">Opel</option>
                    <option value="audi">Audi</option>-->
                </select>
            </span>
        </div>
        <!--<p style="text-align:center;font-size:40px;color:cadetblue;background-color:antiquewhite;margin-top:0px">分组列表</p>-->

        <table id="tbParas" class="table" style="margin-top:0px;">
            <tr>
                <th style="width:80px;">序号</th>
                <th style="width:200px;">组名称</th>
                <th style="width:170px;">Key</th>
                <th style="width:200px;">参数名称</th>
                <th style="width:300px;">参数描述</th>
                <th style="width:400px;">值</th>
            </tr>
        </table>

        <button id="btnParaValueOk" class="AddButton">确定</button>
    </div>
</body>
</html>
